iT邦幫忙

2024 iThome 鐵人賽

DAY 10
0
Python

Django - 製作網頁一點通系列 第 10

Day10 - Python基礎(三)

  • 分享至 

  • xImage
  •  

這篇將透過5個實例來說明python的語法。

將以下程式保存為01.py後執行

  • for迴圈

for 變數 in 數列:
程式

for i in range(2,10,3):
    print(i, end=' ')

在python中,使用for和range做一定次數的迴圈
https://ithelp.ithome.com.tw/upload/images/20240924/20169478RidbXMN7RT.png

將以下程式保存為02.py後執行

  • break命令

在迴圈中使用以結束迴圈

for i in range(10):
    print(i, end=' ')
    if i==5:
        break

在python中,使用break來跳出迴圈
https://ithelp.ithome.com.tw/upload/images/20240924/20169478MO91agKAFd.png

將以下程式保存為03.py後執行

  • continue命令

在迴圈中,跳過當次迴圈,繼續執行下一個迴圈

for i in range(10):
    if i==5:
        continue
    print(i, end=' ')

在python中,使用continue來跳過一次的迴圈,進入下一次迴圈
https://ithelp.ithome.com.tw/upload/images/20240924/20169478R2OOOy7R8T.png

將以下程式保存為04.py後執行

  • while迴圈

while 條件:
程式

n = 0
while n<10:
    n += 1
    print(n, end=' ') 

在python中,使用while進行迴圈,直到滿足條件
https://ithelp.ithome.com.tw/upload/images/20240924/20169478uM7rcdtRGH.png

將以下程式保存為05.py後執行

  • list串列的建立和呼叫

串列 = [元素1, 元素2, ..., 元素n]

fruits = ['蘋果', '西瓜', '草莓', '芭樂', '酪梨']
print(fruits[0], fruits[4])

在python中,使用list建立串列,並使用中括號來呼叫其中的元素
https://ithelp.ithome.com.tw/upload/images/20240924/20169478UhxwAKYBMF.png


上一篇
Day9 - Python基礎(二)
下一篇
Day11 - Python基礎(四)
系列文
Django - 製作網頁一點通12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言